package com.siysoft.tc.classroom.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.siysoft.tc.base.bean.bo.AvatarBo;
import com.siysoft.tc.base.bean.bo.ClassroomBo;
import com.siysoft.tc.base.bean.bo.ClassroomDetailBo;
import com.siysoft.tc.base.bean.bo.ClassroomUserBo;
import com.siysoft.tc.base.bean.bo.MemberBo;
import com.siysoft.tc.base.bean.bo.PaginationBo;
import com.siysoft.tc.base.bean.bo.RoomChatBo;
import com.siysoft.tc.base.bean.property.ResourceType;
import com.siysoft.tc.base.bean.to.Reservation;
import com.siysoft.tc.base.bean.to.Resource;
import com.siysoft.tc.base.bean.to.RoomChat;
import com.siysoft.tc.base.bean.to.User;
import com.siysoft.tc.base.bean.to.UserReservation;
import com.siysoft.tc.base.bean.to.VUserReservation;
import com.siysoft.tc.base.bean.to.VUserRoomChat;
import com.siysoft.tc.classroom.service.IClassroomService;
import com.siysoft.util.COMMON;
import com.tc.base.dao.BaseDAO;

@Service
public class ClassroomServiceImpl implements IClassroomService{
	@Autowired
	BaseDAO baseDao;
	
	public String getResource(int data_id, String type) {
		String hql= "from Resource a where a.dataType='"+type+"' AND a.dataId="+data_id;
		List<Resource> resourcelist =baseDao.findByHQL(hql);
		Resource resource1=new Resource();
		if(resourcelist.size()!=0){
			return resourcelist.get(0).getImgPath();
		}
		return null;
		
	}
	
	@Override
	public List<ClassroomBo> getClassroom(Integer id, PaginationBo pb) {
		// TODO Auto-generated method stub
		try{
			//String hql = "from VUserReservation where user_id="+id+" and reservation_status=0";
			String hql = "from VUserReservation where user_id="+id;
			List<VUserReservation> lvur = (List<VUserReservation>) baseDao.findByHQL(hql, null, pb.getPagination_offset(), pb.getPagination_limit());
			pb.setPagination_total(baseDao.findByHQL(hql).size());
			List<ClassroomBo> lcb = new ArrayList<ClassroomBo>();
			for(Integer i = 0; i < lvur.size(); i++){
				VUserReservation vur = lvur.get(i);
				Integer classroom_id = vur.getReservation_id();
				String classroom_course = vur.getReservation_course();
				String classroom_teacher_name = vur.getReservation_lecture();
				List<AvatarBo> lab = new ArrayList<AvatarBo>();
				String hql2 = "from UserReservation where reservation_id="+classroom_id;
				List<UserReservation> lur = (List<UserReservation>)baseDao.findByHQL(hql2, null, 0, 3);
				for(Integer j = 0; j < lur.size(); j ++){
					UserReservation ur = lur.get(j);
					int avatar_user_id = ur.getUser_id();
					String avatar_url = COMMON.NulltoAvatar(this.getResource(avatar_user_id,ResourceType.USER_AVATAR));
					String avatar_name = ((User)baseDao.findById(User.class, avatar_user_id)).getUser_name();
					AvatarBo ab = new AvatarBo(avatar_user_id, avatar_url,avatar_name);
					lab.add(ab);
				}
				PaginationBo classroom_pagination = pb;
				ClassroomBo cb = new ClassroomBo(classroom_id, classroom_course, 
						classroom_teacher_name, lab, classroom_pagination, vur.getReservation_status());
				lcb.add(cb);
			}
			return lcb;
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public ClassroomDetailBo getClassroomDetail(Integer classroom_id, Integer user_id) {
		// TODO Auto-generated method stub
		String hql = "from Reservation where reservation_id="+classroom_id;
		List<Reservation> r = baseDao.findByHQL(hql);
		String reservation_lecture = "";
		if(r.get(0).getReservation_lecture_id() != -1){
			reservation_lecture = ((User)baseDao.findById(User.class, r.get(0).getReservation_lecture_id())).getUser_name();
		}
		Integer classroom_detail_id = classroom_id;
		String classroom_detail_reservation_number = r.get(0).getReservation_number();
		String classroom_detail_school = r.get(0).getReservation_school();
		String classroom_detail_lecture = reservation_lecture;
		Integer classroom_detail_max_size = r.get(0).getReservation_max_size();
		Integer classroom_detail_current_size = r.get(0).getReservation_current_size();
		String classroom_detail_address = r.get(0).getReservation_address();
		Timestamp classroom_detail_time = r.get(0).getReservation_time();
		Integer classroom_detail_period = r.get(0).getReservation_period();
		Integer classroom_detail_goal = r.get(0).getReservation_goal();
		double classroom_detail_wage = r.get(0).getReservation_wage();
		double classroom_detail_bonus = r.get(0).getReservation_bonus();
		Integer classroom_detail_public = r.get(0).getReservation_ispublic();
		Integer classroom_detail_create_id = r.get(0).getReservation_create_id();
		String classroom_detail_course = r.get(0).getReservation_course();
		String hql1 = "from UserReservation where user_id="+user_id+" and reservation_id="+classroom_id;
		List<UserReservation> lur = baseDao.findByHQL(hql1);
		Integer user_reservation_status = lur.get(0).getUser_reservation_status();
		//System.out.println(classroom_detail_time);
		ClassroomDetailBo cdb = new ClassroomDetailBo(classroom_detail_id,
				classroom_detail_reservation_number, classroom_detail_school,
				classroom_detail_lecture, classroom_detail_max_size,
				classroom_detail_current_size, classroom_detail_address,
				classroom_detail_time, classroom_detail_period,
				classroom_detail_goal, classroom_detail_wage,
				classroom_detail_bonus, classroom_detail_public,
				classroom_detail_create_id, classroom_detail_course, user_reservation_status);
		return cdb;
	}

	@Override
	public List<MemberBo> getMember(Integer id, PaginationBo pb) {
		// TODO Auto-generated method stub
		try{
			String hql = "from VUserReservation where reservation_id="+id;
			List<VUserReservation> lvur = (List<VUserReservation>) baseDao.findByHQL(hql, null, pb.getPagination_offset(), pb.getPagination_limit());
			pb.setPagination_total(baseDao.findByHQL(hql).size());
			List<MemberBo> lmb = new ArrayList<MemberBo>();
			for(Integer i = 0; i < lvur.size(); i++){
				VUserReservation vur = lvur.get(i);
				int member_classroom_id = vur.getReservation_id();
				int member_user_id = vur.getUser_id();
				String member_name = vur.getUser_name();
				String member_avatar_url = COMMON.NulltoAvatar(this.getResource(member_user_id,ResourceType.USER_AVATAR));;
				String member_telephone = vur.getUser_telephone();
				Integer member_pay_state = vur.getUser_reservation_status();
				Integer member_type = vur.getUser_type();
				Integer member_wage = vur.getUser_reservation_wage();
				Integer member_period = vur.getUser_reservation_period();
				/*if(vur.getUser_name().equals(vur.getReservation_lecture())){
					member_type = 1;
				}*/
				MemberBo mb = new MemberBo(member_classroom_id,
						member_user_id, member_name, member_avatar_url,
						member_telephone, member_pay_state, member_type, member_wage, member_period);
				lmb.add(mb);
			}
			return lmb;
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public ClassroomUserBo getMemberInfo(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer quit(Integer classroom_id, Integer user_id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer chat_send(Integer reservation_id, Integer user_id, String content, String to_user_name) {
		// TODO Auto-generated method stub
		String hql = "from User where user_name='" + to_user_name + "'";
		List<User> lu = baseDao.findByHQL(hql);
		Integer to_user_id = -1;
		if(lu.size() > 0) to_user_id = lu.get(0).getUser_id();
		
		RoomChat rc = new RoomChat();
		rc.setRoom_chat_reservation_id(reservation_id);
		rc.setRoom_chat_user_id(user_id);
		rc.setRoom_chat_content(content);
		rc.setRoom_chat_time(new Timestamp(System.currentTimeMillis()));
		rc.setRoom_chat_to_user_id(to_user_id);
		baseDao.save(rc);
		return 1;
	}

	@Override
	public List<RoomChatBo> chat_list(Integer reservation_id) {
		// TODO Auto-generated method stub
		String hql = "from VUserRoomChat where room_chat_reservation_id="+reservation_id+" order by room_chat_time";
		List<VUserRoomChat> lvurc = baseDao.findByHQL(hql);
		List<RoomChatBo> lrcb = new ArrayList<RoomChatBo>();
		for(Integer i = 0; i < lvurc.size(); i++){
			VUserRoomChat vurc = lvurc.get(i);
			int room_chat_id = vurc.getRoom_chat_id();
			int room_chat_user_id = vurc.getRoom_chat_user_id();
			String room_chat_user_name = vurc.getRoom_chat_user_name();
			String room_chat_content = vurc.getRoom_chat_content();
			String room_chat_user_avatar = COMMON.NulltoAvatar(this.getResource(room_chat_user_id,ResourceType.USER_AVATAR));
			RoomChatBo rcb = new RoomChatBo(room_chat_id, room_chat_user_id,
					room_chat_user_name, room_chat_content, room_chat_user_avatar);
			lrcb.add(rcb);
		}
		return lrcb;
	}

	@Override
	public Integer update(Integer id, String school, String lecture,
			Integer max_size, String address, Timestamp time, Integer period,
			Integer goal, Integer wage, Integer bonus) {
		Reservation r = (Reservation)baseDao.findById(Reservation.class, id);
		String reservation_lecture = ((User)baseDao.findById(User.class, r.getReservation_lecture_id())).getUser_name();
		if(r.getReservation_current_size() > max_size){
			return -1;
		}
		else if(!reservation_lecture.equals(lecture)){
			String hql = "from User where user_name='"+lecture+"'";
			List<User> lu = baseDao.findByHQL(hql);
			if(lu.size() == 0){
				return -2;
			}
			else{
				Integer after_lecture_id = lu.get(0).getUser_id();
				String hql3 = "from UserReservation where user_id="+after_lecture_id+" and reservation_id="+id;
				List<UserReservation> lur1 = baseDao.findByHQL(hql3);
				if(lur1.size() == 0){
					UserReservation ur = new UserReservation();
					ur.setUser_id(after_lecture_id);
					ur.setReservation_id(id);
					ur.setUser_reservation_status(0);
					baseDao.save(ur);
				}
				
				Integer ini_lecture_id = r.getReservation_lecture_id();
				String hql2 = "from UserReservation where user_id="+ini_lecture_id+" and reservation_id="+id;
				List<UserReservation> lur = baseDao.findByHQL(hql2);
				baseDao.delete(lur.get(0));
				
				String hql4 = "from User where user_name='"+lecture+"'";
				List<User> lus = baseDao.findByHQL(hql4);
				r.setReservation_lecture_id(lus.get(0).getUser_id());
				baseDao.save(r);
				return 1;
			}
		}
		else{
			r.setReservation_school(school);
			r.setReservation_max_size(max_size);
			r.setReservation_address(address);
			r.setReservation_time(time);
			r.setReservation_period(period);
			r.setReservation_goal(goal);
			r.setReservation_wage(wage);
			r.setReservation_bonus(bonus);
			baseDao.save(r);
			return 1;
		}
	}

	@Override
	public List<MemberBo> getMemberByType(Integer id, PaginationBo pb, Integer user_type) {
		// TODO Auto-generated method stub
		try{
			String hql = "from VUserReservation where reservation_id=" + id + " and user_type=" + user_type;
			List<VUserReservation> lvur = (List<VUserReservation>) baseDao.findByHQL(hql, null, pb.getPagination_offset(), pb.getPagination_limit());
			pb.setPagination_total(baseDao.findByHQL(hql).size());
			List<MemberBo> lmb = new ArrayList<MemberBo>();
			for(Integer i = 0; i < lvur.size(); i++){
				VUserReservation vur = lvur.get(i);
				int member_classroom_id = vur.getReservation_id();
				int member_user_id = vur.getUser_id();
				String member_name = vur.getUser_name();
				String member_avatar_url = COMMON.NulltoAvatar(this.getResource(member_user_id,ResourceType.USER_AVATAR));;
				String member_telephone = vur.getUser_telephone();
				Integer member_pay_state = vur.getUser_reservation_status();
				Integer member_type = 0;
				if(vur.getUser_name().equals(vur.getReservation_lecture())){
					member_type = 1;
				}
				Integer member_wage = vur.getUser_reservation_wage();
				Integer member_period = vur.getUser_reservation_period();
				MemberBo mb = new MemberBo(member_classroom_id,
						member_user_id, member_name, member_avatar_url,
						member_telephone, member_pay_state, member_type, member_wage, member_period);
				lmb.add(mb);
			}
			return lmb;
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Integer agreeTeacher(Integer classroom_id, Integer user_id) {
		// TODO Auto-generated method stub
		
		String hql1 = "from UserReservation where reservation_id=" + classroom_id + " and user_id=" + user_id;
		List<UserReservation> lur = baseDao.findByHQL(hql1);
		
		String hql = "from Reservation where reservation_id=" + classroom_id;
		List<Reservation> lr = baseDao.findByHQL(hql);
		Reservation r = lr.get(0);
		if(r.getReservation_lecture_id() == user_id){
			return 0;
		}
		r.setReservation_lecture_id(user_id);
		r.setReservation_wage(lur.get(0).getUser_reservation_wage());
		r.setReservation_period(lur.get(0).getUser_reservation_period());
		baseDao.save(r);
		return 1;
	}

}
